﻿using System;
using System.Collections.Generic;
using System.Text;
using Yuebon.Commons.Cache;
using Yuebon.Commons.Encrypt;
using Yuebon.Security.Dtos;
using Yuebon.Security.IServices;
using Yuebon.Security.Models;

namespace Yuebon.IdentityServer.SSO
{
    public class SSOAuthHelper
    {
        private ISystemTypeService systemTypeService;
        private IAPPService aPPService;
        private IUserService userService;
        private IUserLogOnService userLogOnService;

        public SSOAuthHelper(ISystemTypeService _systemTypeService, IAPPService _aPPService, IUserService _userService, IUserLogOnService _userLogOnService)
        {
            systemTypeService = _systemTypeService;
            aPPService = _aPPService;
            userService = _userService;
            userLogOnService = _userLogOnService;
        }
        /// <summary>
        /// 登录验证
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public LoginResult Validate(PassportLoginRequest model)
        {
            var result = new LoginResult();
            try
            {
                model.IsValid();
                //获取应用信息
                var app = aPPService.GetAPP(model.AppId,model.AppSecret);
                if (app == null)
                {
                    throw new Exception("应用不存在");
                }
                //获取用户信息
                User userInfo = new User();
                userInfo = userService.GetByUserName(model.Account);
                
                if (userInfo == null)
                {
                    throw new Exception("用户不存在");
                }
                var userSinginEntity = userLogOnService.GetByUserId(userInfo.Id);
                string inputPassword = MD5Util.GetMD5_32(DEncrypt.Encrypt(MD5Util.GetMD5_32(model.Password).ToLower(), userSinginEntity.UserSecretkey).ToLower()).ToLower();

                if (userSinginEntity.UserPassword!= inputPassword)
                {
                    throw new Exception("密码错误");
                }

                var currentSession = new UserAuthSession
                {
                    UserId= userInfo.Id,
                    Account = model.Account,
                    Name = userInfo.RealName,
                    Token = Guid.NewGuid().ToString().GetHashCode().ToString("x"),
                    AppKey = model.AppId,
                    CreateTime = DateTime.Now
                    //,IpAddress = HttpContext.Current.Request.UserHostAddress
                };

                YuebonCacheHelper yuebonCacheHelper = new YuebonCacheHelper();
                //创建Session
                yuebonCacheHelper.Add(currentSession.Token, currentSession);

                result.ErrCode = "200";
                result.Success = true;
                result.ReturnUrl = app.RequestUrl;
                result.Token = currentSession.Token;
            }
            catch (Exception ex)
            {
                result.ErrCode = "500";
                result.ErrMsg = ex.Message;
            }

            return result;
        }
    }
}
